// pages/singerIndex/singerIndex.js
var app = getApp();
var util = require("../../utils/util");
var util_md5 = require("../../utils/md5.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isAlbum: false, //  是否为专辑歌单详情
    isLoading: true, //  是否需要加载
    isEnd: true, //  是否是页面到底
    isPlaying: false, //  是否能点击播放（用于阻止用户频繁/连续点播）
    isAdding: false, //  是否能点击添加（用于阻止用户频繁/连续添加）
    albumscroll: true, //  是否为专辑类型滚动翻页

    listCover: "", //  专题封面
    listData: [], //  专题歌单列表

    url: "", //  歌手歌单/综艺歌单接口地址
    pageNumber:1 , //  当前页
    pageCount: 1, //  当前专题总页数

    openid: "", //  用户身份凭证
    uid: "", //  设备idhttp://singerimg.cavca.net/刘德华.jpg", //歌手头像
    name: "", //歌手名称
    singerid: "", //歌手id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, "888");
    var that = this;
    var orderType;
    var category = options.category;
    var name = options.name;
    var imgurl = options.imgurl;
    var singerid = options.id;
    
    that.setData({
      imgurl: imgurl,
      name: name,
      singerid: singerid,
     
    });
    let params = {
      filter: [
        { matchType: "like", matchName: "singer", matchValue: that.data.name },
      ],
      per_page: 20,
      page: that.data.pageNumber,
    };
    that.setData({
      params:params
    })
    wx.setNavigationBarTitle({
      title: name,
    });
    that.requestList();
  },
  requestList() {
    var that = this;
    wx.request({
      url: util.api + "/songs",
      method: "POST",
      data: JSON.stringify(that.data.params),
      success(res) {
        if (res.statusCode == 200) {
          console.log(res.data.data, "000");
          that.setData({
            listData: that.data.listData.concat(res.data.data.data),
            pageNumber: res.data.data.current_page,
            pageCount: res.data.data.last_page,
          });
        }
      },
    });
  },
  bindNavView: function () {
    // 跳转前需要把歌曲名称传过去
    let data = "";
    let that = this;
    data = JSON.stringify(that.data.listData.slice(0, 6));
    wx.navigateTo({
      url:
        "../singerIndexSearch/singerIndexSearch?imgurl=" +
        that.data.imgurl +
        "&name=" +
        that.data.name +
        "&singerid=" +
        that.data.singerid +
        "&data=" +
        data,
    });
  },

  /**
   * @description: 点击播放歌曲
   */
  play(e) {
    var that = this;
    console.log(e.currentTarget.dataset.code);
    var token = wx.getStorageSync("token");
    var code = e.currentTarget.dataset.code;
    var tempParam = {
      code: code,
    };
    if (token) {
      util.postToken("/wechat/play", tempParam, token, function (res) {
        console.log(res.status_code, "播放");
        if (res.status_code==200) {
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 500
          })
        } else {
          
        }
      });
    }
  },

  /**
   * @description: 点击添加歌曲
   */
  add(e) {
    var that = this;
    var codes = e.currentTarget.dataset.code;
    var uid =  app.globalData.uid;
    var token = wx.getStorageSync("token");
    var headurl = wx.getStorageSync("wxImg");
    var tempParam = {
      codes: codes,
      uid: uid,
      headurl: headurl,
    };
    if (token) {
      util.postToken("/wechat/playlist/song/add", tempParam, token, function (
        res
      ) {
        console.log(res,'添加');
      });
    } else {
    }
  },

  /**
   * @description: 点击收藏歌曲
   */
  love(e) {
    var that = this;
    var songCode = e.currentTarget.dataset.code;
    var token = wx.getStorageSync("token");
    var tempParam = {
      songCode: songCode,
    };
    if (token) {
      util.postToken("/wechat/song/favorites/add", tempParam, token, function (
        res
      ) {
        console.log(res, "收藏");
      });
    }
  },

 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("到底");
    var that = this;
    if (
      that.data.pageNumber < that.data.pageCount &&
      that.data.pageCount >= 2
    ) {
      setTimeout(function () {
        var pageNumber = Number(that.data.pageNumber) + 1;
        let params = {
          filter: [
            { matchType: "like", matchName: "singer", matchValue: that.data.name },
          ],
          per_page: 20,
          page: pageNumber,
        };
        that.setData({
          params
        })
        that.requestList()
      }, 1000);
      clearTimeout();
    } else {
      that.setData({
        isEnd: true,
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
